<%@ page language="java" contentType="text/html;charset=UTF-8"%>
<%@ include file="/commons/taglibs.jsp"%>
<%@ include file="/commons/tree.jsp"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
<script language="JavaScript" type="text/javascript" src="${ctx}/scripts/ajaxException.js"></script>
<script language="JavaScript" src="${ctx}/scripts/common/prototype.js"></script>
<script type="text/javascript" src="${ctx}/scripts/common.js"></script>
<script language="JavaScript"  src="${ctx}/scripts/public.js"></script>
<script type="text/javascript"
	src="${ctx}/scripts/dhtmlxTree/dhtmlXDataProcessor.js"></script>
<script language="javascript">
	var COMPANY_ACTION="${ctx}/house_companyAction.do?";
	var MANAGEORGMAIN_ACTION="${ctx}/house_manageOrgAction.do?";
	var tree;//定义树

	//当选择树点时触发的事件
	function selectItem(itemId){
		if( tree.getLevel(itemId) == 2){
			$('mainFrame').src =MANAGEORGMAIN_ACTION + "method=initManageOrgMain&manageOrgId="+getItemId(itemId);
			processButtonStatus(false); //处理按钮状态
	    }else if( tree.getLevel(itemId) == 1){
			$('mainFrame').src =MANAGEORGMAIN_ACTION + "method=initCompanyMain&companyId="+getItemId(itemId);
			processButtonStatus(true); //处理按钮状态
	    }
   }

	//因树组件生成第一节点时，有可能会在真正的ID前加一串数字以“_”隔开
	function getItemId(itemId){
		if(itemId.indexOf("_")>-1){
			return itemId.substring(itemId.indexOf("_")+1,itemId.length);
		}else{
			return itemId;
		}
	}

	//树组件的右键菜单
   function rightClickItem(itemId){
	    tree.selectItem(itemId);
	    if(tree.getLevel(itemId)==2){
			hidden2menu('quick_rightmenu')
			ShowContextMenu(event,'quick_rightmenu2');
	    } else if(tree.getLevel(itemId)==1){
			hidden2menu('quick_rightmenu2')
			ShowContextMenu(event,'quick_rightmenu');
	    }

   }

	//树生成后调用本方法
	function callbackTree(){
		tree.openItem();
		tree.selectItem();
		processButtonStatus(true);//设置删除按钮状态为不可用
		var companyId = tree.getSelectedItemId();   //id获得
		$('mainFrame').src =  MANAGEORGMAIN_ACTION + "method=initCompanyMain&companyId="+companyId;
	}

	//页面加载时，初始树
	function initTree(){
		tree=new dhtmlXTreeObject("house_tree","100%","100%",0);
		tree.setImagePath("${ctx}/images/tree/");
		tree.setOnClickHandler(selectItem);
		tree.setOnRightClickHandler(rightClickItem);
		//tree.setXMLAutoLoading(MANAGEORGMAIN_ACTION + "method=getCompanyTreeData");
		tree.loadXML(MANAGEORGMAIN_ACTION + "method=getCompanyTreeData",callbackTree);
	}

	//新建管理处
	function openNewManageOrg(){
		var url=MANAGEORGMAIN_ACTION + "method=initManageOrgAdd";
		top.windowOpen(url, 'newManageOrg', '${key.addManageOrg}', 380, 380, true, "manageOrgEditIFrame");
	}

	//修改管理处
	function openEditManageOrg(){
		if(tree.getLevel(tree.getSelectedItemId())==2){
			id=getItemId(tree.getSelectedItemId());
			var url=MANAGEORGMAIN_ACTION + "method=initManageOrgEdit&manageOrgId="+id;
			top.windowOpen(url, 'newManageOrg', '${key.editManageOrg}', 380, 380,true, "manageOrgEditIFrame");
		}else if(tree.getLevel(tree.getSelectedItemId())==1){
			openEditCompany();
		}else{
			ErrorAlert('${key.tipsByModify}');
		}
	}

	//弹出删除管理处提示窗口
	function showDelete(){
		if( tree.getLevel(tree.getSelectedItemId()) != 2 ){
			ErrorAlert('${key.tipsByDelete}');
		}else{
			AlarmConfirm('${key.tipsByDeleteConfirm}',"main","deleteManageOrg");
		}
	}
	//执行删除
	function deleteManageOrg(){
		var url=MANAGEORGMAIN_ACTION + "method=deleteManageOrg&manageOrgId="+getItemId(tree.getSelectedItemId());
		var myAjax = new Ajax.Request(
	                   url,
	                   {method: 'post', parameters:"" , onComplete: showResponseDelete}
	                   );
	}
	//删除响应
	function showResponseDelete(transport){
		ajaxSubmitException(transport.responseText);
		if(transport.responseText=="false"){
			FailureAlert('${key.doFalse}');
		}else{
			refreshTree();
		}
	}


	//刷新树,如果有text则会根据text自动选择该节点，否则默认选择第一个结点
	function refreshTree(text){
		tree.deleteChildItems(tree.rootId);
		tree.loadXML(MANAGEORGMAIN_ACTION + "method=getCompanyTreeData",function (){
			tree.openItem();
			if(text == undefined){
				tree.selectItem();
				//tree.focusItem(tree.rootId);
			}else{
				tree.findItem(text,0,1);
			}
			refreshPage();
		});
	}

	//如果当前选定的是一级节点，则刷新右页面为公司管理页面,如果是二级节点，则刷新右页面为管理处页面
	function refreshPage(){
		var id = getItemId(tree.getSelectedItemId());   //id获得
		if( tree.getLevel(tree.getSelectedItemId()) == 1 ){
			$('mainFrame').src =  MANAGEORGMAIN_ACTION + "method=initCompanyMain&companyId="+id;
			processButtonStatus(true); //处理按钮状态
		}else if( tree.getLevel(tree.getSelectedItemId()) == 2 ){
			$('mainFrame').src =  MANAGEORGMAIN_ACTION + "method=initManageOrgMain&manageOrgId="+id;
		}
	}

	//如果当前选择的是公司节点则返回该节点，如果选择的是管理处节点，则返回其父节点
	function getCurrentCompanyId(){
		if(tree.getLevel(tree.getSelectedItemId())==1){
			return tree.getSelectedItemId();
		}else if(tree.getLevel(tree.getSelectedItemId())==2){
			return tree.getParentId(tree.getSelectedItemId());
		}
	}

	//修改物业公司
	function openEditCompany(){
		if( !tree.getSelectedItemId()
			|| tree.getSelectedItemId()==""
			|| tree.getLevel(tree.getSelectedItemId())!= 1)
		{
			ErrorAlert('${key.tipsByModify}');
		}else{
			var url=COMPANY_ACTION + "method=initCompanyEdit&companyId="+ getItemId(tree.getSelectedItemId());
			top.windowOpen(url, 'editCompany', '${key.modifyCompany}', 325, 360,true, "companyEditIFrame");
		}
	}
	//设置按钮状态,flag为true时,新建按钮不做任何修改,删除按钮不可用
	//flag为flase时,新建按钮不做任何修改,删除按钮可用
	function processButtonStatus(flag){
		if(flag){
			document.getElementById("deleteButton").onclick = null;
			document.getElementById("deleteButton").disabled = true;
		}else{
			document.getElementById("deleteButton").onclick = showDelete;
		}
	}
</script>
</head>

<body onload="initTree();">

<table width="100%" border="0" align="center" cellpadding="0"
	cellspacing="0">
	<tr>
		<td height="48">
		<div class="pageNav_bg">
		<table width="100%" border="0" cellspacing="0" cellpadding="0">
			<tr>
				<td id="pageNav_leftimg"><img
					src="${ctx}/images/ico_fc_quyu.gif" width="46" height="47" /></td>
				<td id="navWord"><span class="page_title">${key.manageSetup}</span></td>
				<td class="pageNav_right"><a href="javascript:showFull_in();"><img
					id="full_image" src="${ctx}/images/ico_fullscreen.gif" border="0" /></a></td>
			</tr>
		</table>

		</div>
		</td>
	</tr>
</table>
<div class="container2" valign="top">
<table width="100%" border="0" cellspacing="0" cellpadding="0">

	<tr>
		<td width="19%" valign="top">

		<table width="100%" valign="right" border="0" align="right"
			cellpadding="0" cellspacing="0">
			<tr>
				<td valign="top">
				<table width="100%" border="0" cellpadding="0" cellspacing="0"
					class="outside_border">
					<tr>
						<td valign="top">
						<table width="100%" height="477" border="0" cellpadding="0"
							cellspacing="0" id=topadcontent_10>

							<tr>
								<td class="bg_toolBar">
								<table width="100%" border="0" cellspacing="0" cellpadding="0">
									<tr>
										<td width="40%">&nbsp;</td>
										<td width="60%" align="right"><!--------------------------右边图标01----------------------------------->
										<table border="0" cellpadding="0" cellspacing="0" id="ico3">
											<tr>
												<td class="toolbarico"><a href="#"
													onFocus="this.blur()" onClick="openNewManageOrg();"><img
													src="${ctx}/images/ico_newfile.gif" alt="${key.add}" width="16"
													height="16" border="0" align="absbottom" /></a></td>
												<td class="toolbarico"><a id="editButton" href="#"
													onclick="openEditManageOrg();" onfocus="this.blur()"><img
													src="${ctx}/images/ico_amend.gif" border="0"
													align="absbottom" alt="${key.modify}" />&nbsp;</a></td>
												<td class="toolbarico"><a id="deleteButton" href="#"
													onClick="showDelete();" onFocus="this.blur()"><img
													src="${ctx}/images/ico_delete.gif" alt="${key.delete}" width="16"
													height="16" border="0" align="absbottom"></a></td>
											</tr>
										</table>
										</td>
									</tr>
								</table>
								</td>
							</tr>

							<tr>
								<td valign="top">

								<div class="treediv">
								<div id="house_tree"
									style="overflow:auto;width:100%;height:620px;"></div>
								</div>
								</td>
							</tr>
						</table>
						</td>
					</tr>
				</table>
				</td>
			</tr>
		</table>
		</td>
		<td align="center" valign="top"><iframe src="" name="mainFrame"
			id="mainFrame" title="mainFrame" width="100%" height="650px"
			frameborder="0" border="0" scroll="no"></iframe></td>
	</tr>
</table>
</div>


<!--------------------左边右键 公司相关菜单 开始---------------------->
<div id="quick_rightmenu" style="display:none;"
	oncontextmenu="return false" class="quick_rightmenu">
<table width="100%" border="0" cellpadding="0" cellspacing="0"
	class="grid_quickMenu_bg">
	<tr>
		<td>
		<table width="100%" border="0" cellpadding="2" cellspacing="0">

			<tr>
				<td class="quickMenu_toolbarico"><a href="#"
					onclick="openEditCompany();"
					onfocus="this.blur()"><img src="${ctx}/images/ico_amend.gif"
					border="0" align="absbottom" alt="${key.modifyCompany}" />&nbsp;${key.modifyCompany}</a></td>
			</tr>

			<tr>
				<td class="quickMenu_toolbarico"><a href="#"
					onclick="openNewManageOrg();;" onfocus="this.blur()"><img
					src="${ctx}/images/ico_newfile.gif" border="0" align="absbottom"
					alt="${key.addManageOrg}" />&nbsp;${key.addManageOrg}</a></td>
			</tr>
		</table>
		</td>
	</tr>
</table>
</div>
<!--------------------左边右键 管理处相关菜单 开始-------------------->
<div id="quick_rightmenu2" style="display:none;"
	oncontextmenu="return false" class="quick_rightmenu">
<table width="100%" border="0" cellpadding="0" cellspacing="0"
	class="grid_quickMenu_bg">
	<tr>
		<td>
		<table width="100%" border="0" cellpadding="2" cellspacing="0">

			<tr>
				<td class="quickMenu_toolbarico"><a href="#"
					onclick="openEditManageOrg();" onfocus="this.blur()"><img
					src="${ctx}/images/ico_amend.gif" border="0" align="absbottom"
					alt="${key.editManageOrg}" />&nbsp;${key.editManageOrg}</a></td>
			</tr>
			<tr>
				<td class="quickMenu_toolbarico"><a href="#"
					onClick="showDelete();"
					onFocus="this.blur()"><img src="${ctx}/images/ico_delete.gif"
					alt="" width="16" height="16" border="0" align="absbottom"> ${key.delete}</a></td>
			</tr>
		</table>
		</td>
	</tr>
</table>
</div>

</body>
</html>

